package com.inossem.warehousestorage_inossem.activity.refund_storage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.refundStorage.RefundStorageOperatorListAdapter;
import com.inossem.warehousestorage_inossem.bean.refundStorage.RefundStorageOverviewBean;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.RefundStorageUtils;

public class RefundStorageAddOperatorActivity extends BaseActivity {

    private TextView title, nextStep;

    private PullableListView operatorList;

    private RefundStorageOperatorListAdapter adapter;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_refund_storage_add_operator);
        initView(RefundStorageAddOperatorActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(RefundStorageAddOperatorActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        operatorList = (PullableListView) activity.findViewById(R.id.operatorList);
    }

    private void initData(Activity activity) {
        title.setText("退库经办人");
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (RefundStorageUtils.hasOperator(activity)) {
                    Intent intent = new Intent(activity, RefundStorageConfirmActivity.class);
                    startActivity(intent);
                } else {
                    Utils.middleToast(activity, activity.getResources().getString(R.string.activity_receive_add_receiver_fill_in_receiver));
                }
            }
        });
        ActivityUtils.initBackAndFunction(activity, "概览", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        operatorList.setPullUpFlag(false);
        operatorList.setPullDownFlag(false);
        getData(activity);
    }

    private void getData(Context context) {
        RefundStorageOverviewBean.DataBean data = RefundStorageUtils.getRefundStorageOverviewBean(context).getData();
        addReceiverListData(context, data, operatorList);
    }

    private void addReceiverListData(Context context, RefundStorageOverviewBean.DataBean data, ListView listView) {
        adapter = new RefundStorageOperatorListAdapter(context, data, () -> {
            initData(RefundStorageAddOperatorActivity.this);
        });
        listView.setAdapter(adapter);
    }

    public interface RefundStorageAddOperatorListener {
        void onDeleteOperatorListener();
    }

}
